package com.example.homecunter.news;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.utils.BaseEntity;
import com.example.homecunter.BR;
import com.example.homecunter.R;
import com.example.homecunter.news.adapter.NewsAdapter;
import com.example.homecunter.news.newsentity.NewsEntity;
import com.example.homecunter.news.newsviewmodel.NewsViewModel;
import com.example.mvvmcore.fragment.BaseMVVMFragment;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Map;

/**
 * 新闻展示
 */
public class NewsSunFragment extends BaseMVVMFragment<NewsViewModel> {
    View view;

    NewsAdapter newsAdapter;
    private RecyclerView homeSunRv;
    private SmartRefreshLayout newsSmart;
    int page = 1;
    List<NewsEntity> newsData;
    private TextView homeTvGx;


    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull @NotNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        view = super.onCreateView(inflater, container, savedInstanceState);
        initView();
        return view;
    }

    @Override
    protected void initData() {
        /**
         * 获取发送方的数据 并展示数据
         */
        int type = this.getArguments().getInt("type");
        mViewModel.getNews(type, page).observe(this, new Observer<BaseEntity<List<NewsEntity>>>() {
            @Override
            public void onChanged(BaseEntity<List<NewsEntity>> listBaseEntity) {

                newsData = listBaseEntity.getData();
                newsAdapter = new NewsAdapter(getContext(), newsData);
                homeSunRv.setLayoutManager(new LinearLayoutManager(getContext()));
                homeSunRv.setAdapter(newsAdapter);

            }
        });

        newsSmart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                mViewModel.getNews(type, page).observe(NewsSunFragment.this, new Observer<BaseEntity<List<NewsEntity>>>() {
                    @Override
                    public void onChanged(BaseEntity<List<NewsEntity>> listBaseEntity) {

                        newsData = listBaseEntity.getData();
                        newsAdapter = new NewsAdapter(getContext(), newsData);
                        homeSunRv.setLayoutManager(new LinearLayoutManager(getContext()));
                        homeSunRv.setAdapter(newsAdapter);

                    }
                });
                newsSmart.finishLoadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
//                newsData.clear();
                page = 1;
                mViewModel.getNews(type, page).observe(NewsSunFragment.this, new Observer<BaseEntity<List<NewsEntity>>>() {
                    @Override
                    public void onChanged(BaseEntity<List<NewsEntity>> listBaseEntity) {

                        newsData = listBaseEntity.getData();
                        newsAdapter = new NewsAdapter(getContext(), newsData);
                        homeSunRv.setLayoutManager(new LinearLayoutManager(getContext()));
                        homeSunRv.setAdapter(newsAdapter);

                    }
                });
                newsSmart.finishRefresh();

            }
        });

        homeTvGx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                page++;
                mViewModel.getNews(type, page).observe(NewsSunFragment.this, new Observer<BaseEntity<List<NewsEntity>>>() {
                    @Override
                    public void onChanged(BaseEntity<List<NewsEntity>> listBaseEntity) {

                        newsData = listBaseEntity.getData();
                        newsAdapter = new NewsAdapter(getContext(), newsData);
                        homeSunRv.setLayoutManager(new LinearLayoutManager(getContext()));
                        homeSunRv.setAdapter(newsAdapter);

                    }
                });
            }
        });

    }

    @Override
    protected Class getViewModelClazz() {
        return NewsViewModel.class;
    }

    @Override
    protected void initEnv() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_news_sun;
    }

    @Override
    protected void preparVarMap(Map<Integer, Object> map) {
        map.put(BR.sun, this);
    }

    @Override
    protected void stopLoad() {

    }

    @Override
    protected void lazyLoad() {

    }

    private void initView() {
        homeSunRv = (RecyclerView) view.findViewById(R.id.home_sun_rv);
        newsSmart = (SmartRefreshLayout) view.findViewById(R.id.news_smart);
        homeTvGx = (TextView) view.findViewById(R.id.home_tv_gx);
    }


    //    private TextView tv;
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        View inflate = inflater.inflate(R.layout.fragment_news_sun, container, false);
//
//        tv = (TextView) inflate.findViewById(R.id.tv);
//        String name = this.getArguments().getString("title");
//        tv.setText(name);
//        Toast.makeText(getContext(), "+"+name, Toast.LENGTH_SHORT).show();
//        return inflate;
//    }

}